<?
if(!defined ('U_MOD'))
{
	echo"<meta http-equiv='refresh' content='0;url=/index.php'>";
	exit;
}
$showForm=1;

if(isset($mkey) && $mkey!='' && !is_numeric($mkey))
	$addToLink='&mkey='.$mkey;
else
	$addToLink='';


include 'topmenu.php';

if (!isset($FormError))
{
	$i=0;
	$FormError=array();
}

if (isset($act) && ($act==1 || $act==2) || $act==5)
{
	if ($act==1){$ActionUrl=1;  $ActionButton=_ADDNEW_;}
	if ($act==2){$ActionUrl=2;  $ActionButton=_SAVE_; $midActionUrl='&pid='.$pid.$addToLink;}
	if ($act==5){$ActionUrl=5;  $ActionButton=_SAVE_; $midActionUrl='&pid='.$pid.$addToLink;}

	if ($act==1 || $act==2 || $act==5)
	{
		if($act==1)
			$queryParametrCheck=mysql_query("SELECT * FROM ".$PortalPref."options WHERE option_parametr='".$pvar."'");
		if($act==2 || $act==5)
			$queryParametrCheck=mysql_query("SELECT * FROM ".$PortalPref."options WHERE option_id='".$pid."'");
		if (mysql_num_rows($queryParametrCheck)!=0 && $act==1)
		{
			$FormError[$i]=_PARAMETR_EXIST_;
			$i++;
		}elseif(mysql_num_rows($queryParametrCheck)==0 && $act==2)
		{
			$FormError[$i]=_NO_PARAMETR_FOR_EDITE_;
			$i++;
			$showForm=0;
			echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToLink."'>";
		}elseif(mysql_num_rows($queryParametrCheck)==0 && $act==5)
		{
			$FormError[$i]=_NO_PARAMETR_FOR_EDITE_;
			$i++;
			$showForm=0;
			echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToLink."'>";
		}else
		{
			if ($act==2 && (!isset($pid) || !is_numeric($pid)))
			{
				$FormError[$i]=_NO_PARAMETR_FOR_EDITE_;
				$i++;
				$showForm=0;
				echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToLink."'>";
			}

			if ($act==5 && (!isset($pid) || !is_numeric($pid)))
			{
				$FormError[$i]=_NO_PARAMETR_FOR_EDITE_;
				$i++;
				$showForm=0;
				echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToLink."'>";
			}

			if ($act==2)
			{
				$resultParametrCheck=mysql_fetch_assoc($queryParametrCheck);

				$parametr_id=$resultParametrCheck['option_id'];
				$parametr_name=$resultParametrCheck['option_name'];
				$parametr_variant=$resultParametrCheck['option_permissible'];
				$parametr_type=$resultParametrCheck['option_type'];

				if ($resultParametrCheck['option_type']=='txt')
				{
					if (!isset($pvalue) || $pvalue=='' || empty($pvalue))
					{
						$FormError[$i]=_NO_PARAMETR_VALUE_;
						$i++;
					}
				}
			}

			if ($act==1 || $act==5)
			{
				if (!isset($mkey) || $mkey=='' || is_numeric($mkey) || empty($mkey))
				{
					$FormError[$i]=_NO_MODULE_KEY_;
					$i++;
				}

				if (!isset($ptype) || $ptype=='' || is_numeric($ptype) || empty($ptype))
				{
					$FormError[$i]=_NO_PARAMETR_TYPE_;
					$i++;
				}

				if (!isset($pname) || $pname=='' || empty($pname))
				{
					$FormError[$i]=_NO_PARAMETR_NAME_;
					$i++;
				}else
					$pname=trim($pname);

				if (!isset($pvar) || $pvar=='' || empty($pvar))
				{
					$FormError[$i]=_NO_PARAMETR_VAR_;
					$i++;
				}else
					$pvar=trim($pvar);

				if (!isset($pvalue) || $pvalue=='' || empty($pvalue))
				{
					$FormError[$i]=_NO_PARAMETR_VALUE_;
					$i++;
				}else
					$pvalue=trim($pvalue);

				if(isset($ptype) && !is_numeric($ptype) && $ptype!='txt')
				{
					if (!isset($pvariant) || $pvariant=='' || empty($pvariant))
					{
						$FormError[$i]=_NO_PARAMETR_VARIANT_;
						$i++;
					}else
						$pvariant=trim($pvariant);
					if (!isset($pdesc) || $pdesc=='' || empty($pdesc))
					{
						$FormError[$i]=_NO_PARAMETR_VARIANT_DESC_;
						$i++;
					}else
						$pdesc=trim($pdesc);
				}
			}

			if (empty($FormError))
			{
				if ($act==1)
				{
					if (!isset($prequred) || empty($prequred) || $prequred=='')
						$prequred=0;
					else
						$prequred=$prequred;

					if (!is_numeric($ptype) && $ptype!='txt')
						$variantImplode=$pvariant."|".$pdesc;
					else
						$variantImplode='';

					$queryParametr="INSERT INTO ".$PortalPref."options
						(
							option_id,
							option_mkey,
							option_name,
							option_parametr,
							option_value,
							option_permissible,
							option_requred,
							option_type
						)VALUES
						(
							NULL,
							'".$mkey."',
							'".$pname."',
							'".$pvar."',
							'".$pvalue."',
							'".$variantImplode."',
							'".$prequred."',
							'".$ptype."'
						)";

					if ($prequred==1 && $mkey!='portal')
					{
						$queryCreateModulesList=mysql_query("SELECT modules_key FROM ".$PortalPref."modules WHERE modules_key <> '".$mkey."' GROUP BY modules_key");
						if(mysql_num_rows($queryCreateModulesList)!=0)
						{
							while($resultCreateModulesList=mysql_fetch_assoc($queryCreateModulesList))
							{
								mysql_query("INSERT INTO ".$PortalPref."options
								(
									option_id,
									option_mkey,
									option_name,
									option_parametr,
									option_value,
									option_permissible,
									option_requred,
									option_type,
									option_sys
								)VALUES
								(
									NULL,
									'".$resultCreateModulesList['modules_key']."',
									'".$pname."',
									'".$pvar."',
									'".$pvalue."',
									'".$variantImplode."',
									'".$prequred."',
									'".$ptype."',
									'0'
								)");
							}
						}
					}
				}elseif($act==5)
				{
					$result=mysql_fetch_assoc($queryParametrCheck);

					if (!isset($prequred) || empty($prequred) || $prequred=='')
						$prequred=0;
					else
						$prequred=$prequred;

					$variantArray='';

					if (!is_numeric($ptype) && $ptype!='txt')
						$variantImplode=$pvariant."|".$pdesc;
					else
						$variantImplode='';

					if($result['option_requred']==0 && $prequred==0)
					{
						$queryParametr="UPDATE ".$PortalPref."options SET
							option_mkey='".$mkey."',
							option_name='".$pname."',
							option_parametr='".$pvar."',
							option_value='".$pvalue."',
							option_permissible='".$variantImplode."',
							option_requred='".$prequred."',
							option_type='".$ptype."'
						WHERE
							option_id='".$pid."'";
					}elseif($result['option_requred']==1 && $prequred==1)
					{
						$queryParametr="UPDATE ".$PortalPref."options SET
							option_name='".$pname."',
							option_parametr='".$pvar."',
							option_value='".$pvalue."',
							option_permissible='".$variantImplode."',
							option_requred='".$prequred."',
							option_type='".$ptype."'
						WHERE
							option_parametr='".$result['option_parametr']."' AND option_requred='1'";
					}elseif($result['option_requred']==1 && $prequred==0)
					{
						$queryParametr= "INSERT INTO ".$PortalPref."options
							(
								option_id,
								option_mkey,
								option_name,
								option_parametr,
								option_value,
								option_permissible,
								option_requred,
								option_type,
								option_sys
							)VALUES
							(
								NULL,
								'".$mkey."',
								'".$pname."',
								'".$pvar."',
								'".$pvalue."',
								'".$variantImplode."',
								'".$prequred."',
								'".$ptype."',
								'1'
							)";

						mysql_query("DELETE FROM ".$PortalPref."options WHERE option_parametr='".$result['option_parametr']."' AND option_requred='1'");
					}elseif($result['option_requred']==0 && $prequred==1)
					{
						$queryParametr= "UPDATE ".$PortalPref."options SET
							option_name='".$pname."',
							option_parametr='".$pvar."',
							option_value='".$pvalue."',
							option_permissible='".$variantImplode."',
							option_requred='".$prequred."',
							option_type='".$ptype."'
						WHERE
							option_id='".$pid."'";

						$queryCreateModulesList=mysql_query("SELECT modules_key FROM ".$PortalPref."modules WHERE modules_key <> '".$mkey."' GROUP BY modules_key");

						if(mysql_num_rows($queryCreateModulesList)!=0)
						{
							while($resultCreateModulesList=mysql_fetch_assoc($queryCreateModulesList))
							{
								mysql_query("INSERT INTO ".$PortalPref."options
								(
									option_id,
									option_mkey,
									option_name,
									option_parametr,
									option_value,
									option_permissible,
									option_requred,
									option_type,
									option_sys
								)VALUES
								(
									NULL,
									'".$resultCreateModulesList['modules_key']."',
									'".$pname."',
									'".$pvar."',
									'".$pvalue."',
									'".$variantImplode."',
									'".$prequred."',
									'".$ptype."',
									'1'
								)");
							}
						}
					}
				}elseif($act==2)
				{
					if ($resultParametrCheck['option_type']=='chkbx')
					{
						if (!isset($pvalue) || empty($pvalue) || $pvalue=='')
							$pvalue=0;
						else
							$pvalue=$pvalue;
					}

					$queryParametr="UPDATE ".$PortalPref."options
					SET
						option_value='".$pvalue."'
					WHERE
						option_id='".$pid."'
					";
				}

				if (mysql_query($queryParametr))
				{
					$showForm=0;
					print '<p align="center">'._OPERATION_SUCCESS_.'</p>';
					echo"<meta http-equiv='refresh' content='0;url=/admin.php?amk=".$amk.$addToLink."'>";
					//exit;
				}else
				{
					if($act==1 && $prequred==1)
					{
						mysql_query("DELETE FROM ".$PortalPref."options WHERE option_parametr='".$pvar."' AND option_requred='1'");
					}
					$FormError[$i]=_BAD_SQL_;
					$i++;
				}
			}
		}
	}
}elseif(isset($act) && $act==3)
{
	$ActionUrl=2;  $ActionButton=_SAVE_;
	$queryOptionSelect=mysql_query("SELECT * FROM ".$PortalPref."options WHERE option_id='".$pid."'");
	if (mysql_num_rows($queryOptionSelect)!=1)
	{
		$FormError[$i]=_NO_PARAMETR_FOR_EDITE_;
		$i++;
		$showForm=0;
		echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToLink."'>";
	}else
	{
		while($resultOptionSelect=mysql_fetch_assoc($queryOptionSelect))
		{
			$parametr_id=$resultOptionSelect['option_id'];
			$parametr_name=$resultOptionSelect['option_name'];
			$pvalue=$resultOptionSelect['option_value'];
			$parametr_variant=$resultOptionSelect['option_permissible'];
			$parametr_type=$resultOptionSelect['option_type'];
		}
		$midActionUrl='&pid='.$pid.$addToLink;
	}
}elseif(isset($act) && $act==4)
{
	$ActionUrl=5;  $ActionButton=_SAVE_;
	$queryOptionSelect=mysql_query("SELECT * FROM ".$PortalPref."options WHERE option_id='".$pid."'");
	if (mysql_num_rows($queryOptionSelect)!=1)
	{
		$FormError[$i]=_NO_PARAMETR_FOR_EDITE_;
		$i++;
		$showForm=0;
		echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToLink."'>";
	}else
	{
		while($resultOptionSelect=mysql_fetch_assoc($queryOptionSelect))
		{
			$pname=$resultOptionSelect['option_name'];
			$pvalue=$resultOptionSelect['option_value'];
			$mkey=$resultOptionSelect['option_mkey'];
			$ptype=$resultOptionSelect['option_type'];
			$pvar=$resultOptionSelect['option_parametr'];
			$prequred=$resultOptionSelect['option_requred'];
			$variantArray=explode('|', $resultOptionSelect['option_permissible']);
			$pvariant=$variantArray[0];
			$pdesc=$variantArray[1];
		}
		$midActionUrl='&pid='.$pid.$addToLink;
	}

}elseif(isset($act) && $act==6)
{
	$showForm=0;
	$queryOptionSelect=mysql_query("SELECT * FROM ".$PortalPref."options WHERE option_id='".$pid."'");
	if (mysql_num_rows($queryOptionSelect)!=1)
	{
		$FormError[$i]=_NO_PARAMETR_FOR_EDITE_;
		$i++;
		echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToLink."'>";
	}else
	{
		$resultOptionSelect=mysql_fetch_assoc($queryOptionSelect);
		if($resultOptionSelect['optyon_sys']!=1)
		{
			if($resultOptionSelect['option_requred']==1)
				mysql_query("DELETE FROM ".$PortalPref."options WHERE option_parametr='".$resultOptionSelect['option_parametr']."' AND option_requred='".$resultOptionSelect['option_requred']."'");
			else
				mysql_query("DELETE FROM ".$PortalPref."options WHERE option_id='".$pid."'");
			print '<p align="center">'._OPERATION_SUCCESS_.'</p>';
			echo"<meta http-equiv='refresh' content='0;url=/admin.php?amk=".$amk.$addToLink."'>";
		}else
		{
			$FormError[$i]=_CANT_DEL_SYS_PARAMETR_;
			$i++;
			echo"<meta http-equiv='refresh' content='2;url=/admin.php?amk=".$amk.$addToLink."'>";
		}
	}
}else
{
	$ActionUrl=1;  $ActionButton=_ADDNEW_;
}

print '<p>';
getErrorList($FormError);
print '</p>';

if (!isset($ptype) || is_numeric($ptype) || empty($ptype))
	$type=-1;
else
	$type=$ptype;

if (isset($prequred) && $prequred==1)
	$requred_flag='checked';
else
	$requred_flag='';


if ($showForm==1)
{
	print '<form method="post" name="SettingsFrm" action="'.$portal_url.'/admin.php?amk='.$amk.'&apk='.$apk.'&act='.$ActionUrl.''.$midActionUrl.'">
		<table width="100%" align="center">';
			if($ActionUrl==1 || $act==4 || $act==5)
			{
				if (!isset($mkey) || empty($mkey) || $mkey=='' || is_numeric($mkey))
					$moduleSelected='portal';
				else
					$moduleSelected=$mkey;

				print '<tr>
					<td>'._MODULE_.'</td>
					<td>';
						GetModuleList($moduleSelected,0,'mkey',_FOR_ALL_,'','');
					print'</td>
				</tr>
				<tr>
					<td>'._OPTION_PARAMETR_TYPE_.'</td>
					<td>
						<select name="ptype">
							'.GetOptionTypeList($type).'
						</select>
					</td>
				</tr>';
			}
			print'<tr>
				<td>';
					if ($act==2 || $act==3)
						print pEcho($parametr_name);
					else
						print _OPTION_PARAMETR_NAME_;
				print'</td>
				<td>';
					if ($act==2 || $act==3)
						print GetOptionTypeVaLue($pvalue, $parametr_variant, $parametr_type, 'form');
					else
						print '<input type="text" name="pname" value="'.$pname.'">';
				print'</td>
			</tr>';
			if($ActionUrl==1 || $act==4 || $act==5)
			{
				print'<tr>
					<td>'._OPTION_PARAMETR_VAR_.'</td>
					<td>
						<input type="text" name="pvar" value="'.$pvar.'">
					</td>
				</tr>
				<tr>
					<td>'._OPTION_PARAMETR_VALUE_.'</td>
					<td>
						<textarea cols="25" rows="3" name="pvalue">'.$pvalue.'</textarea>
					</td>
				</tr>
				<tr>
					<td>'._OPTION_PARAMETR_VALUE_VARIANT_.'</td>
					<td>
						<textarea cols="25" rows="3" name="pvariant">'.$pvariant.'</textarea>
					</td>
				</tr>
				<tr>
					<td>'._OPTION_PARAMETR_VALUE_VARIANT_DESC_.'</td>
					<td>
						<textarea cols="25" rows="3" name="pdesc">'.$pdesc.'</textarea>
					</td>
				</tr>
				<tr>
					<td>'._OPTION_PARAMETR_REQURED_.'</td>
					<td>
						<input type="checkbox" name="prequred" value="1" '.$requred_flag.'>
					</td>
				</tr>';
			}
			print'<tr>
				<td colspan="2"><input type="submit" value="'.$ActionButton.'"></td>
			</tr>
		</table>
	</form>';
}
?>